<?php

namespace App\Http\Controllers\Api\O1\Pay;

use Illuminate\Http\Request;
use Biz\Services\Site\SitePayService;
use Biz\Services\Site\SiteOrderService;
use Biz\Constants\Site\SiteOrderConstants;
use Lumen\Payment\PayEase\Facades\PayEase;
use Lumen\Core\Exceptions\BusinessException;
use Lumen\Http\Resources\Json\DataResourceResponse;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * PayEaseController.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class PayEaseController extends BaseController
{
    /**
     * @var mixed
     */
    protected $payService;
    /**
     * @var mixed
     */
    protected $orderService;

    /**
     * PayController constructor.
     *
     * @param SitePayService   $payService
     * @param SiteOrderService $orderService
     */
    public function __construct(SitePayService $payService, SiteOrderService $orderService)
    {
        $this->payService   = $payService;
        $this->orderService = $orderService;
    }

    /**
     * 首信易支付.
     *
     * @param Request $request
     * @param string  $order_no
     *
     * @throws BusinessException
     *
     * @return \Illuminate\Http\Response
     */
    public function pay(Request $request, string $order_no)
    {
        // 获取网站支付配置
        $site_id        = $request->get('site_id');
        $paySetting     = $this->payService->findBySiteId($site_id);
        if (is_null($paySetting)) {
            throw new BusinessException(trans('pay/pay-ease.config_not_found'));
        }

        // 获取易支付配置
        $payEaseConfig = $paySetting->pay_ease;
        if (is_null($payEaseConfig)) {
            throw new BusinessException(trans('pay/pay-ease.config_not_found'));
        }

        // 获取订单
        $order           = $this->orderService->findByOrderNo($order_no);
        $orderUser       = $order->user;
        $orderUserMember = $order->user->member;
        $orderName       = null;
        switch ($order->type) {
            case SiteOrderConstants::TYPE_REGISTRATION_FEE:
                $orderName = $order->registration_fee->fee_name;
                break;
            case SiteOrderConstants::TYPE_HOTEL_RESERVATION:
                $orderName = $order->hotel_reservation->hotel_name.$order->hotel_reservation->room_type_name;
                break;
            default:
                $orderName = 'Unknown';
        }

        // 检测订单所有者
        $user = $request->user();
        if ($user->id !== $orderUser->id) {
            throw new BusinessException(trans('pay/pay-ease.order_not_found'));
        }

        // 检测站点
        $site_id = $request->get('site_id');
        if ($order->site_id !== $site_id) {
            throw new BusinessException(trans('pay/pay-ease.order_not_found'));
        }

        // 支付币种 0：人民币，1：美元

        $moneyType   = 0;
        $moneyLocale = 'zh-CN';
        if ('USD' === $order->currency) {
            //$moneyType = 1;//TODO
            //$moneyLocale = 'en';//TODO
        }

        $options = [
            'order_no'   => $order->order_no,
            'order_name' => $orderName,
            'rcv_name'   => $orderUserMember->first_name.' '.$orderUserMember->last_name,
            'rcv_email'  => $orderUser->email,
            'rcv_addr'   => $orderUserMember->address,
            'rcv_tel'    => $orderUserMember->phone_number,
            'rcv_post'   => '',
            'ymd'        => date('Ymd', time()),
            'money'      => 0.01, //money_format('%i', $order->total_amount),
            'money_type' => $moneyType,
            'return_url' => 'http://cc-api.weixiaodeyu.com:8090/notify/site/pay/ease/sync',
        ];

        PayEase::configure($payEaseConfig->toArray(), $language = $moneyLocale);
        $redirect = PayEase::pay($options);

        // 更新状态为待付款
        $this->orderService->wait($order);

        return new DataResourceResponse($redirect);
    }
}
